/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import java.io.ByteArrayInputStream;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;
import uber.netbsd.community.dao.gallery.GalleryImage;
import uber.netbsd.community.dao.gallery.GalleryManager;

/**
 * Action to download images from gallery
 *
 * @author alex
 */
@UrlBinding("/action/GetImage")
public class GetImageActionBean extends BaseActionBean {

    private Integer imageId; // image id

    public Integer getImageId() {
        return imageId;
    }

    public void setImageId(Integer imageId) {
        this.imageId = imageId;
    }

    @ValidationMethod
    public void validate(ValidationErrors errors) {

        if (GalleryManager.getInstance().getGalleryImage(imageId) == null) {
            errors.add("user.username", new SimpleError("IMAGE NOT FOUND"));
        }

    }

    /**
     * User tried to delete image
     * @return redirect url
     */
    @HandlesEvent("delete")
    public Resolution cancelChanges() {

         GalleryManager gm = GalleryManager.getInstance();
         gm.delete(imageId);
         

        return new RedirectResolution("/gallery");
    }

    /**
     *
     * @return image preview (as stream)
     */
    @HandlesEvent("preview")
    public Resolution getPreviewImage() {
        GalleryManager gm = GalleryManager.getInstance();
        GalleryImage g = gm.getGalleryImage(this.imageId);



        // Uses a StreamingResolution to send the file contents back to the user.
        // Note the use of the chained .setFilename() method, which causes the
        // browser to [prompt to] save the "file" instead of displaying it in browser
        return new StreamingResolution("image/png", new ByteArrayInputStream(g.getPreview())).setFilename(g.getName() + "-preview.png");

    }

    /**
     *
     * @return full image (as stream)
     */
    @DefaultHandler
    public Resolution getFullImage() {
        GalleryManager gm = GalleryManager.getInstance();
        GalleryImage g = gm.getGalleryImage(this.imageId);



        // Uses a StreamingResolution to send the file contents back to the user.
        // Note the use of the chained .setFilename() method, which causes the
        // browser to [prompt to] save the "file" instead of displaying it in browser
        return new StreamingResolution(g.getContentType(), new ByteArrayInputStream(g.getData())).setFilename(g.getName() + ".png");

    }
}
